/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *
 * @author Muhammad Anis
 */
public class fileProcessor {

    //variable2
    public File file;
    //konstruktor

    public fileProcessor(File f) {
        this.file = f;
    }

    public fileProcessor(String path) {
        this.file = new File(path);
    }

    public byte[] getByteFromFile() {
        try {
            //create FileInputStream object
            FileInputStream fin = new FileInputStream(file);

            /*
             * Create byte array large enough to hold the content of the file.
             * Use File.length to determine size of the file in bytes.
             */
            byte fileContent[] = new byte[(int) file.length()];

            /*
             * To read content of the file in byte array, use
             * int read(byte[] byteArray) method of java FileInputStream class.
             *
             */
            fin.read(fileContent);

            //create string from byte array
            fin.close();           
            return fileContent;

        } catch (FileNotFoundException e) {
            System.out.println("File not found" + e);
        } catch (IOException ioe) {
            System.out.println("Exception while reading the file " + ioe);
        }
        return null;

    }

    public void writeToFile(byte[] b) {
        try {
            FileOutputStream fos = new FileOutputStream(this.file);
            //String strContent = "Write File using Java FileOutputStream example !";

            /*
             * To write byte array to a file, use
             * void write(byte[] bArray) method of Java FileOutputStream class.
             *
             * This method writes given byte array to a file.
             */

            fos.write(b);

            /*
             * Close FileOutputStream using,
             * void close() method of Java FileOutputStream class.
             *
             */

            fos.close();

        } catch (FileNotFoundException ex) {
            System.out.println("FileNotFoundException : " + ex);
        } catch (IOException ioe) {
            System.out.println("IOException : " + ioe);
        }

    }
}
